<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import serachUserid from '@/components/serachUserid/index.vue';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    formType:1
  })
  interface formQuert {
    reason:string
    user_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    reason: [{ required: true, trigger: 'blur', message: '请填写封禁原因' }],
    user_id: [{ required: true, trigger: 'blur', message: '请输入用户UUID' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { formType } = action
      const loading = Toastloading()
      let submitType = false
      if(formType == 1) {
        await numberApi.blackListAdd(form).then(()=>{
          ToastSuccess('封禁成功')
          submitType = true
        })
      }
      if(formType == 2) {
        await numberApi.blackListDelete(form).then(()=>{
          ToastSuccess('解封成功')
          submitType = true
        })
      }
      if(submitType) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const { 
        formType=1,
        nickname='',
        user_id=0,
        reason=''
    } = query
    if(formType == 1) {
      action.title = '封禁用户'
    }
    if(formType == 2) {
      Object.assign(form,{
        user_id,
        reason
      })
      action.title = `解封:${nickname}`
    }
    action.formType = formType
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="60%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item v-if="action.formType == 1" :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <el-form-item v-if="action.formType == 2" :label-width="formLabelWidth" label="解封原因" prop="reason">
          <el-input
            :disabled="action.disabled"
            v-model="form.reason"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请填写封禁原因"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>